package org.wang.generalcode.modules.sys.service.impl;

import cn.hutool.captcha.AbstractCaptcha;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.wang.generalcode.modules.sys.constant.CacheConstants;
import org.wang.generalcode.modules.sys.pojo.dto.CaptchaDTO;
import org.wang.generalcode.modules.sys.pojo.enums.CaptchaTypeEnum;
import org.wang.generalcode.modules.sys.mapper.LoginMapper;
import org.wang.generalcode.modules.sys.service.ILoginService;
import org.wang.generalcode.support.base.BaseEntity;
import org.wang.generalcode.support.config.properties.CaptchaProperties;

import java.awt.*;
import java.util.concurrent.TimeUnit;


/**
 * 登录 服务实现类
 *
 * @author wz
 * @since 2024-08-09
 */
@Service
@RequiredArgsConstructor
public class LoginServiceImpl extends ServiceImpl<LoginMapper, BaseEntity> implements ILoginService {

    private final CodeGenerator codeGenerator;
    private final Font captchaFont;
    private final CaptchaProperties captchaProperties;
    private final RedisTemplate<String, Object> redisTemplate;

    @Override
    public CaptchaDTO getCaptcha() {
        String captchaType = captchaProperties.getType();
        int width = captchaProperties.getWidth();
        int height = captchaProperties.getHeight();
        int interfereCount = captchaProperties.getInterfereCount();
        int codeLength = captchaProperties.getCode().getLength();

        AbstractCaptcha captcha;
        if (CaptchaTypeEnum.CIRCLE.name().equalsIgnoreCase(captchaType)) {
            captcha = CaptchaUtil.createCircleCaptcha(width, height, codeLength, interfereCount);
        } else if (CaptchaTypeEnum.GIF.name().equalsIgnoreCase(captchaType)) {
            captcha = CaptchaUtil.createGifCaptcha(width, height, codeLength);
        } else if (CaptchaTypeEnum.LINE.name().equalsIgnoreCase(captchaType)) {
            captcha = CaptchaUtil.createLineCaptcha(width, height, codeLength, interfereCount);
        } else if (CaptchaTypeEnum.SHEAR.name().equalsIgnoreCase(captchaType)) {
            captcha = CaptchaUtil.createShearCaptcha(width, height, codeLength, interfereCount);
        } else {
            throw new IllegalArgumentException("Invalid captcha type: " + captchaType);
        }
        captcha.setGenerator(codeGenerator);
        captcha.setTextAlpha(captchaProperties.getTextAlpha());
        captcha.setFont(captchaFont);

        String captchaCode = captcha.getCode();
        String imageBase64Data = captcha.getImageBase64Data();


        // 验证码文本缓存至Redis，用于登录校验
        String captchaKey = IdUtil.fastSimpleUUID();

//        ehCacheUtil.put(CacheConstants.CAPTCHA_CODE_PREFIX, captchaKey, captchaCode, Math.toIntExact(captchaProperties.getExpireSeconds()));
        redisTemplate.opsForValue().set(CacheConstants.CAPTCHA_CODE_PREFIX + captchaKey, captchaCode,
                captchaProperties.getExpireSeconds(), TimeUnit.SECONDS);

        return CaptchaDTO.builder().captchaKey(captchaKey).captchaBase64(imageBase64Data).build();
    }
}
